package com.xinsoft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.CusRepairApproveDTO;
import com.xinsoft.entity.po.CusDeviceBreakdown;
import com.xinsoft.entity.po.CusRepairApprove;
import com.xinsoft.entity.po.CusRepaire;
import com.xinsoft.entity.po.SysUser;
import com.xinsoft.mapper.CusRepairApproveMapper;
import com.xinsoft.service.*;
import com.xinsoft.utils.TokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author hsm
* @description 针对表【cus_repair_approve(维修验收)】的数据库操作Service实现
* @createDate 2023-06-25 17:07:00
*/
@Service
public class CusRepairApproveServiceImpl extends ServiceImpl<CusRepairApproveMapper, CusRepairApprove>
    implements CusRepairApproveService{

    @Resource
    public CusDeviceBreakdownService cusDeviceBreakdownService;

    @Resource
    public CusRepaireService cusRepaireService;

    @Resource
    public SysDictionaryService sysDictionaryService;

    @Resource
    public SysUserService sysUserService;

    @Resource
    public CusRepairApproveService cusRepairApproveService;


    @Override
    public CusRepairApprove addRepairApprove(CusRepairApproveDTO cusRepairApproveDTO) {
        // 校验 此条报修状态是否为待验收，并且不存在验收信息
        CusDeviceBreakdown cusDeviceBreakdown= cusDeviceBreakdownService.getById(cusRepairApproveDTO.getBreakdownId());
        if(!Objects.equals(cusDeviceBreakdown.getRepaireStatus(), Const.RepairStatus.PROCESSED_ACCEPTANCE)){
            // 故障报修非待验收状态，无法验收
            throw new BDException("操作失败，故障报修非待验收状态，无法验收！");
        }

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("breakdown_id",cusRepairApproveDTO.getBreakdownId());
        int count = this.count(queryWrapper);
        if(count != 0){
            throw new BDException("操作失败，故障报修已存在验收消息，请勿重复验收！");
        }
        CusRepairApprove cusRepairApprove = new CusRepairApprove();
        BeanUtils.copyProperties(cusRepairApproveDTO,cusRepairApprove);
        cusRepairApprove.setApprovePersonId(TokenUtil.getUserId());
        cusRepairApprove.setApproveTime(new Date());
        save(cusRepairApprove);

        return cusRepairApprove;
    }

    @Override
    public CusRepairApprove editRepairApprove(CusRepairApproveDTO cusRepairApproveDTO) {
        CusDeviceBreakdown cusDeviceBreakdown= cusDeviceBreakdownService.getById(cusRepairApproveDTO.getBreakdownId());
        if(!Objects.equals(cusDeviceBreakdown.getRepaireStatus(), Const.RepairStatus.PROCESSED_ACCEPTANCE)){
            //故障报修非待验收状态，无法验收
            throw new BDException("操作失败，故障报修非待验收状态，无法验收！");
        }
        CusRepairApprove cusRepairApprove = new CusRepairApprove();
        BeanUtils.copyProperties(cusRepairApproveDTO,cusRepairApprove);
        cusRepairApprove.setApprovePersonId(TokenUtil.getUserId());
        cusRepairApprove.setApproveTime(new Date());
        updateById(cusRepairApprove);
        return cusRepairApprove;
    }

    @Override
    public CusRepairApprove submitRepairApprove(CusRepairApproveDTO cusRepairApproveDTO) {
        CusRepairApprove cusRepairApprove = new CusRepairApprove();
        if(cusRepairApproveDTO.getId()==null){
            cusRepairApprove = cusRepairApproveService.addRepairApprove(cusRepairApproveDTO);
        }else{
            cusRepairApprove = cusRepairApproveService.editRepairApprove(cusRepairApproveDTO);
        }

        Integer breakDownRepairStatus =0;
        Integer recordRepairStatus =0;
        //更新报修状态以及验收结果
        //如果为不通过，则更新报修为待处理，同时更新维修为不通过
        if(Objects.equals(cusRepairApproveDTO.getApproveResult(),Const.AcceptanceResult.RESULT_UNPASS)){
            breakDownRepairStatus =Const.RepairStatus.SUBMIT;
            recordRepairStatus =Const.RepairRecordStatus.FALLBACK;
        }else{
            //验收通过，或者待验收 则更新报修为3-已验收，同时更新维修为通过
            breakDownRepairStatus =Const.RepairStatus.ACCEPTED;
            recordRepairStatus =Const.RepairRecordStatus.PASSED;
        }

        //更新维修
        //获取最后一条维修记录
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("breakdown_id",cusRepairApproveDTO.getBreakdownId());
        queryWrapper.orderByDesc("id");
        List<CusRepaire>  cusRepaireList = cusRepaireService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(cusRepaireList)){
            CusRepaire cusRepaire = new CusRepaire();
            cusRepaire.setId(cusRepaireList.get(0).getId());
            cusRepaire.setRepaireStatus(recordRepairStatus);
            cusRepaireService.updateById(cusRepaire);
        }

        // 更新报修
        CusDeviceBreakdown cusDeviceBreakdown = new CusDeviceBreakdown();
        cusDeviceBreakdown.setId(cusRepairApproveDTO.getBreakdownId());
        cusDeviceBreakdown.setRepaireStatus(breakDownRepairStatus);
        cusDeviceBreakdownService.updateById(cusDeviceBreakdown);

        return cusRepairApprove;
    }


    @Override
    public CusRepairApproveDTO getRepairApprove(Integer id) {

        CusRepairApprove cusRepairApprove = new CusRepairApprove();
        cusRepairApprove = this.getById(id);
        if(cusRepairApprove==null){
            return null;
        }
        //设置结果
        CusRepairApproveDTO cusRepairApproveDTO = new CusRepairApproveDTO();
        BeanUtils.copyProperties(cusRepairApprove,cusRepairApproveDTO);
        cusRepairApproveDTO.setId(cusRepairApprove.getId());
        String resultName = sysDictionaryService.getById(cusRepairApprove.getApproveResult()).getDicName();
        cusRepairApproveDTO.setApproveResultName(resultName);
        //设置验收人
        SysUser user = sysUserService.getById(cusRepairApprove.getApprovePersonId());
        String approvePersonName = user.getRealName();
        cusRepairApproveDTO.setApprovePersonName(approvePersonName);
        return cusRepairApproveDTO;
    }

    @Override
    public CusRepairApproveDTO getRepairApproveByBreakDownId(Integer breakDownId) {
        CusDeviceBreakdown cusDeviceBreakdown= cusDeviceBreakdownService.getById(breakDownId);

        if(!Objects.equals(cusDeviceBreakdown.getRepaireStatus(), Const.RepairStatus.PROCESSED_ACCEPTANCE)
                && !Objects.equals(cusDeviceBreakdown.getRepaireStatus(), Const.RepairStatus.ACCEPTED)){
            return null;
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();

        queryWrapper.select("id");
        queryWrapper.eq("breakdown_id",breakDownId);
        queryWrapper.orderByDesc("id");
        List<CusRepairApprove> list = this.list(queryWrapper);

        if(list.size() ==0){
            return null;
        }

        return getRepairApprove(list.get(0).getId());

    }
}




